<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>

<link rel="stylesheet" href="../css/default.min.css">
<script src="../js/jquery-2.1.3.min.js"></script>
<script src="../js/highlight.min.js"></script>


<style>
html { height: 90%; width: 100%; }
body {
margin: 0;
min-height: 100%;
min-width: 100%;
text-size-adjust: 100%;
-webkit-text-size-adjust: none;
font-size: 12px;
}

body > pre {
margin: 0em;
min-width: 100%;
min-height: 100%;
overflow: none;
}

body > pre * {
overflow: none;
}

.hljs {
width: 100%;
min-width: 100%; min-height: 100%;
box-sizing: border-box;
overflow: none;
padding: 0;
}

code {
display: block;
width: 100%;
min-width: 100%;
padding: 0;
}

.pre-numbering {
/**padding: 0 2px 0 2px;**/
border-right: 1px solid #C3CCD0;
text-align: right;
color: #AAA;
background-color: #EEE;
display: inline-block;
float: left;
margin-top: 0;
margin-right: 15px;
}
</style>

</head>
<body>

<pre><code id="code" class="C">@@ -12,24 +12,23 @@ class GsSearchViewController: UIViewController{
     
     var _tableView: UITableView?
     
-    var _searchResultController:GsSearchResultTableViewController?
+    var _searchResultController:GsSearchResultController?
     var _searchController:UISearchController?
     
-    var _searchBackView: UICollectionView?
-    
     let cellIdentifier = "GsSearchCell"
     
+    var status:UIActivityIndicatorView?
+   
+    
     override func viewWillAppear(animated: Bool) {
         super.viewWillAppear(animated)
-        
         _tableView?.frame = self.view.frame
     }
     
     override func viewDidLoad() {
         super.viewDidLoad()
         
         self.title = sysLang("Discover")
-        
         self.initView()
     }
     
@@ -46,16 +45,22 @@ class GsSearchViewController: UIViewController{
         self.view.addSubview(_tableView!)
         
         
-        _searchResultController = GsSearchResultTableViewController()
-        self._searchController = UISearchController(searchResultsController: _searchResultController)
-        self._searchController!.searchResultsUpdater = self
-        self._searchController!.delegate = self
-        self._searchController!.dimsBackgroundDuringPresentation = false
+        _searchResultController = GsSearchResultController()
+        _searchController = UISearchController(searchResultsController: _searchResultController)
+        _searchController?.searchResultsUpdater = self
+        _searchController?.delegate = self
+        _searchController?.dimsBackgroundDuringPresentation = true
+        
+        _searchController?.searchBar.delegate = self
+        _searchController?.searchBar.placeholder = sysLang("Search Projects")
+        _tableView!.tableHeaderView = self._searchController!.searchBar
         
-        self._searchController!.searchBar.delegate = self
-        self._searchController!.searchBar.placeholder = sysLang("Search Projects")
-        self._tableView!.tableHeaderView = self._searchController!.searchBar
+        _searchController?.hidesNavigationBarDuringPresentation = true
+        self.definesPresentationContext = true
         
+        status = UIActivityIndicatorView(activityIndicatorStyle: .Gray);
+        view.addSubview(status!)
+        status?.startAnimating()
     }
 }
 
@@ -72,72 +77,40 @@ extension GsSearchViewController: UISearchBarDelegate {
     }
     
     func searchBarBookmarkButtonClicked(searchBar: UISearchBar) {
-        print("bookmakr")
+        //print("bookmakr")
     }
     
     func searchBarSearchButtonClicked(searchBar: UISearchBar) {
-        print("staring")
+        //print("staring")
+        _searchResultController?.searchUrl()
     }
 }
 
+//MARK:  - UISearchControllerDelegate & UISearchResultsDelegate -
 extension GsSearchViewController: UISearchControllerDelegate, UISearchResultsUpdating {
     
-    //MARK:  - UISearchControllerDelegate & UISearchResultsDelegate -
     func willPresentSearchController(searchController: UISearchController) {
-        print("willPresentSearchController")
-        
-        
-        
+        //print("willPresentSearchController")
+        self.tabBarController?.tabBar.hidden = true
+        //_searchResultController = GsSearchResultController()
     }
     
     func didPresentSearchController(searchController: UISearchController) {
-        print("didPresentSearchController")
-        //_searchBackView = UIView(frame: CGRectMake(0, 80, 320, 400))
-        //_searchBackView.backgroundColor  = UIColor.grayColor()
-        
-        //self.view.addSubview(_searchBackView)
-//        let top = self._searchController!.searchBar.subviews[0]
-//        for l:UIView in top.subviews {
-//            if(l.isKindOfClass(UIButton)){
-//                let b = l as! UIButton
-//                b.setTitle("取消", forState: UIControlState.Normal)
-//            }
-//        }
-        
-        let layout = UICollectionViewFlowLayout()
-        layout.minimumInteritemSpacing = 0
-        layout.minimumLineSpacing = 0
-        layout.itemSize = CGSizeMake(self.view.frame.width, 40)
-        layout.scrollDirection = UICollectionViewScrollDirection.Vertical
-        
-        _searchBackView = UICollectionView(frame: CGRectMake(0, 0, self.view.frame.width - 100, self.view.frame.height), collectionViewLayout: layout)
-        _searchBackView?.delegate = self
-        _searchBackView?.dataSource = self
-        _searchBackView?.showsVerticalScrollIndicator = true
-    
-        _searchBackView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
-        
-        
-//        let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight)) as UIVisualEffectView
-//        visualEffectView.frame = self.view.bounds
-        //_searchBackView!.addSubview(visualEffectView)
-        
-        _searchBackView?.backgroundColor  = UIColor.whiteColor()
-        self.view.addSubview(_searchBackView!)
+        //print("didPresentSearchController")
     }
     
     func willDismissSearchController(searchController: UISearchController) {
-        
-        _searchBackView?.removeFromSuperview()
+        self.tabBarController?.tabBar.hidden = false
+        _tableView?.frame = self.view.frame
+        _searchResultController?.clearSearchData()
+        _searchResultController?._tableData = Array<JSON>()
     }
     
     //搜索内容更新时
     func updateSearchResultsForSearchController(searchController: UISearchController) {
         let searchText = self._searchController!.searchBar.text
-        
+        //print(searchText)
         if searchText != "" {
-            
-            //print(searchText)
             self._searchResultController?.searchingKeyWords(searchText!)
         }
     }
@@ -161,6 +134,8 @@ extension GsSearchViewController:UITableViewDataSource, UITableViewDelegate{
         return 0
     }
     
+    
+    
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
         
@@ -190,7 +165,6 @@ extension GsSearchViewController:UITableViewDataSource, UITableViewDelegate{
     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         tableView.deselectRowAtIndexPath(indexPath, animated: true)
         
-        
         if indexPath.section == 0 {
         } else if indexPath.section == 1 {
             
@@ -199,6 +173,11 @@ extension GsSearchViewController:UITableViewDataSource, UITableViewDelegate{
                 event.hidesBottomBarWhenPushed = true
                 event.setUrlData("/events")
                 self.push(event)
+            } else if indexPath.row == 1 {
+                let gists = GsGistsViewController()
+                gists.hidesBottomBarWhenPushed = true
+                gists.setUrlData("/gists/public")
+                self.push(gists)
             }
             
         } else if indexPath.section == 2 {
@@ -210,39 +189,15 @@ extension GsSearchViewController:UITableViewDataSource, UITableViewDelegate{
             }
             
         }
-        
     }
 }
 
 extension GsSearchViewController{
     
     override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
+        //_tableView?.frame.origin = CGPointMake(0, -44)
         _tableView?.frame.size = size
     }
-
 }
 
-
-extension GsSearchViewController: UICollectionViewDelegate, UICollectionViewDataSource {
-    
-    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
-        return 1
-    }
-    
-    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
-        return 2
-    }
-    
-    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
-        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as UICollectionViewCell
-        
-        
-        
-        print(indexPath)
-        cell.backgroundColor = UIColor.grayColor()
-        return cell
-    }
-
-    
-}</code></pre>

<script>hljs.initHighlightingOnLoad();</script>
<script>
$(document).ready(function() {
    $('#code').each(function(i, block) {
    	//console.log(i, block);
    	hljs.highlightBlock(block);
	});
});



$(function() {
    hljs.configure({
        tabReplace: '   '
    });

    $('body').css('width', $(document).width() + 75 + 'px');

    hljs.initHighlighting();

    $("#code").css('display','block');

    $('#code').each(function(){
    	//console.log(this);
        var lines = $(this).text().split('\n').length - 1;
        var $numbering = $('<div/>').addClass('pre-numbering');
        var $numbering2 = $('<div/>').addClass('pre-numbering');
        console.log($numbering, $numbering2);
        $(this).addClass('has-numbering').prepend($numbering);
        $(this).addClass('has-numbering').prepend($numbering2);

        for(i=1;i<=lines + 1;i++){
            $numbering.append(document.createTextNode(i + '\n'));
        }

        for(i=1;i<=lines + 1;i++){
            $numbering2.append(document.createTextNode(i + '\n'));
        }
    });
});
</script>
</body>
</html>